﻿using System;

namespace HBattle
{
    public abstract class SingletonManager<T> where T : class, new()
    {
        private static T instance;
        private static int newTimes = 0;

        public static T Instance
        {
            get
            {
                if (instance == null)
                {
                    instance = new T();
                }
                return instance;
            }
        }

        protected SingletonManager()
        {
            newTimes++;
            if (newTimes > 1)
            {
                string typeName = this.GetType().Name;
                throw new Exception(typeName + "类继承单例类SingletonManager,当前New()调用次数为" + newTimes + ",请使用" + typeName + ".Instance");
            }
        }
    }
}